package com.panshi.servers.impl;

import com.panshi.api.util.PageSizeVO;
import com.panshi.mapper.ReviewsMapper;
import com.panshi.pojo.ArticleReviewsVO;
import com.panshi.pojo.PraoseLinkVO;
import com.panshi.servers.ReviewsServers;
import io.swagger.annotations.ApiModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author XiaoWangYa
 * @version 1.0
 * @date 2020/8/4 0004 下午 3:35
 */
@Slf4j
@Service
@ApiModel("评论Servers层")
public class ReviewsServersImpl implements ReviewsServers {

    @Autowired
    private ReviewsMapper yb_reviewsMapper;


    @Override   // 新增评论
    @CacheEvict(value = "reviews", allEntries = true) // 清空缓存
    public int addReviewsInfo(ArticleReviewsVO yb_ArticleReviews_VO) {
        return yb_reviewsMapper.addReviewsInfo(yb_ArticleReviews_VO);
    }

    @Override   // 删除评论
    @CacheEvict(value = "reviews", allEntries = true) // 清空缓存
    public void deleteReviewsInfo(int id) {
        yb_reviewsMapper.deleteReviewsInfo(id);
    }

    @Override   // 查询是否存在父级评论
    @CacheEvict(value = "link", allEntries = true)
    public List<ArticleReviewsVO> queryIFExistSuperId(int sid) {
        return yb_reviewsMapper.queryIFExistSuperId(sid);
    }

    @Override   // 查询指定文章全部评论
    @Cacheable(value = "reviews", key = "#root.methodName + #p0.startIndex + #p0.aid") // 将数据存入缓存
    public List<ArticleReviewsVO> queryAssginArticleReviewsInfo(PageSizeVO yb_PageSize_VO) {
        log.info(" 查询数据库 --> 查询指定文章全部评论 ");
        return yb_reviewsMapper.queryAssginArticleReviewsInfo(yb_PageSize_VO);
    }

    @Override   // 查询指定评论信息
    @Cacheable(value = "reviews", key = "#root.methodName + #id")
    public ArticleReviewsVO queryArticleParise(int id) {
        return yb_reviewsMapper.queryArticleParise(id);
    }

    @Override   // 查询评论回复信息
    @Cacheable(value = "reviews", key = "#root.methodName + #p0.startIndex + #p0.depth")
    public List<ArticleReviewsVO> queryAssginArticleReplyInfo(PageSizeVO pageSizeVO) {
        return yb_reviewsMapper.queryAssginArticleReplyInfo(pageSizeVO);
    }

    // ====== 点赞表 ====== 点赞表 ====== 点赞表 ====== 点赞表 ======

    @Override   // 增加评论点赞量
    @CacheEvict(value = "reviews", allEntries = true) // 清空缓存
    public void addArticlePraise(ArticleReviewsVO articleReviewsVO) {
        yb_reviewsMapper.addArticlePraise(articleReviewsVO);
    }

    @Override   // 增加评论点赞信息
    @CacheEvict(value = "link", allEntries = true)
    public void addLinkInfo(PraoseLinkVO praoseLinkVO) {
        yb_reviewsMapper.addLinkInfo(praoseLinkVO);
    }

    @Override   // 取消评论点赞信息
    @CacheEvict(value = "link", allEntries = true)
    public void cancelArticlePraise(PraoseLinkVO praoseLinkVO) {
        yb_reviewsMapper.cancelArticlePraise(praoseLinkVO);
    }

    @Override   // 查询评论是否被指定用户所点赞
    @Cacheable(value = "link", key = "#root.methodName + #p0.answer_id")
    public PraoseLinkVO queryPraiseLink(PraoseLinkVO praoseLinkVO) {
        return yb_reviewsMapper.queryPraiseLink(praoseLinkVO);
    }
}
